package com.marvin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.marvin.core.User;
import com.marvin.service.IUserService;
import com.marvin.util.MD5Util;
import com.marvin.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author yinyin
 * @create 2021/11/17 下午 2:58
 */
@Controller
public class LoginController {

    @Autowired
    private IUserService userService;

    @PostMapping("logined")
    @ResponseBody
    public String  toLog(@RequestBody UserVo userVo,HttpSession session){
       //判断用户名和密码是否正确
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("telephone",userVo.getAccount());
        User user = userService.getOne(queryWrapper);
        if (user==null){
            return "fail";
        }
        String md5 = MD5Util.md5slat(userVo.getPassword());
        if (user.getPassword().equals(md5)){
            session.setAttribute("SESSION_USER",user);
            return  "success";
        }
        return "fail";

    }

    //退出
    @GetMapping("logout")
    public String logout(HttpSession session){
        // 注销所有的session
        session.invalidate();
        // 2:否则代表没有登录，直接去登录页面
        return "redirect:/login";
    }
    @GetMapping("aaaa")
    public String test(){
        return  "test";
    }
}
